package lcr;

import java.util.List;

public class Test0035 {
    public static void main(String[] args) {
        new Solution0035_1().findMinDifference(List.of("23:59","00:00"));
    }
}

class Solution0035_1 {
    public int findMinDifference(List<String> timePoints) {
        int size = timePoints.size();
        if (size > 1440) return 0;
        int[] record = new int[size];

        for (int i = 0; i < size; i++) {
            String timePoint = timePoints.get(i);
            String[] split = timePoint.split(":");
            int hour = Integer.parseInt(split[0]);
            int minute = Integer.parseInt(split[1]);

            record[i] = hour * 60 + minute;
        }

        quickSort(record, 0, size-1);

        int ans=Integer.MAX_VALUE;
        for (int i = 0; i < size-1; i++){
            ans = Math.min(ans, record[i+1]-record[i]);
        }

        if (record[0] + 1440 - record[size-1] < ans) {
            return record[0] + 1440 - record[size-1];
        } else {
            return ans;
        }


    }

    private void quickSort(int[] record, int left, int right) {
        if (left >= right) return;
        int partition = partition(record, left, right);

        quickSort(record, left, partition-1);
        quickSort(record, partition+1, right);
    }

    private int partition(int[] record, int left, int right) {
        int pivot = record[right];
        int index = left;
        for (int i = left; i < right; i++) {
            if (record[i] < pivot) {
                swap(record, index++, i);
            }
        }
        swap(record, index, right);
        return index;
    }

    private void swap(int[] record, int i, int j) {
        int temp = record[i];
        record[i] = record[j];
        record[j] = temp;
    }
}